<template>
    <div>
        <rui-page :defines="defines">
            <rui-query :label-width="180"
                       ref="searchForm"
                       :query="queryData"
                       :query-rows="searchFormOptions"
                       :more-rows="searchFormOptionsRows"
            >
                <template slot="createTimes" slot-scope="{ key,data }">
                    <DatePicker v-model="data[key]" type="daterange" format="yyyy-MM-dd" clearable
                                @on-change="selectCreateDateRange" placeholder="选择起始时间"
                                style="width: 200px"></DatePicker>
                </template>
                <template slot="endDates" slot-scope="{ key,data }">
                    <DatePicker v-model="data[key]" type="daterange" format="yyyy-MM-dd" clearable
                                @on-change="selectEndDateRange" placeholder="选择起始时间"
                                style="width: 200px"></DatePicker>
                </template>
                <template slot="dispatchTimes" slot-scope="{ key,data }">
                    <DatePicker v-model="data[key]" type="daterange" format="yyyy-MM-dd" clearable
                                @on-change="selectUpdateDateRange" placeholder="选择起始时间"
                                style="width: 200px"></DatePicker>
                </template>
                <template slot="caseAll" slot-scope="{ key,data }">
                    <div>
                        <i-switch size="large" v-model="data[key]" @on-change="changeSwitch"/>
                    </div>
                </template>
            </rui-query>
            <Row>
                <Alert show-icon>
                    已选择 <span class="select-count">{{selectCount}}</span> 项
                    <a class="select-clear" @click="clearSelectAll">清空</a>
                </Alert>
            </Row>
            <rui-table v-if="!isSP"
                       :row-class-name="rowClassName"
                       :defineId="'insuranceManageDefine'"
                       :fixedLeft="['action','contractNo','custName']"
                       :showIndex="false"
                       :select="true"
                       :columns="insuranceManageColumns"
                       :slots="[{key:'action',slot:'action'},{key: 'contractNo',slot: 'contractNo'},{key:'isSend',slot:'isSend'}]"
                       @loadDatas="queryData"
                       ref-query="searchForm"
                       ref="insuranceTable"
                       @on-selection-change="changeSelect"
            >
                <template slot="toolBar">
                    <Button v-has="'button_add'" @click="showModal" icon="md-add" type="primary">新增申请</Button>
                    <!--                    <Button @click="" type="primary">导出报表</Button>-->
                </template>
                <template slot="action" slot-scope="{ row }">
                    <div>
                        <!--
                            处理  isViewBtn ~~审核中 待补件 待补件审核~~
                            编辑 草稿
                            删除 草稿
                        -->
                        <Button v-if="row.processStatus !== 'approved' || row.type === 'insuranceRenew'"
                                :disabled="!row.isViewBtn" type="primary" size="small"
                                @click="edit('write',row)">处理
                        </Button>

                        <Button v-if="row.processStatus !== 'approved' || row.type === 'insuranceRenew'"
                                :disabled="row.processStatus !== 'draft'" type="primary" size="small"
                                @click="edit('write',row)">编辑
                        </Button>

                        <Button v-if="row.processStatus !== 'approved' || row.type === 'insuranceRenew'"
                                :disabled="row.processStatus !== 'draft'" type="error" size="small"
                                @click="remove(row)">
                            删除
                        </Button>
                        <Button v-if="row.processStatus === 'approved' && row.type !== 'insuranceRenew'" type="primary"
                                size="small" @click="PrintTemplate('yes',row)">打印有章
                        </Button>
                        <Button v-if="row.processStatus === 'approved' && row.type !== 'insuranceRenew'" type="primary"
                                size="small" @click="PrintTemplate('no',row)">打印无章
                        </Button>
                    </div>
                </template>
                <template slot="contractNo" slot-scope="{ row }">
                    <a @click="edit('read',row)">{{row.contractNo}}</a>
                </template>
                <template slot="isSend" slot-scope="{ row }">
                    <!--                    <Checkbox :value="row.isSend || false" disabled></Checkbox>-->
                    <Icon v-if="row.isSend" type="md-checkmark" size="16"/>
                </template>
            </rui-table>
            <rui-table v-if="isSP"
                       :defineId="'insuranceManageDefine'"
                       :fixedLeft="['action','contractNo','custName']"
                       :showIndex="false"
                       :select="true"
                       :columns="insuranceManageColumn"
                       :slots="[{key:'action',slot:'action'},{key: 'contractNo',slot: 'contractNo'},{key:'isSend',slot:'isSend'}]"
                       @loadDatas="queryData"
                       ref-query="searchForm"
                       ref="insuranceTable"
                       @on-selection-change="changeSelect"
            >
                <template slot="toolBar">
                    <Button v-has="'button_add'" @click="showModal" icon="md-add" type="primary">新增申请</Button>
                    <!--                    <Button @click="" type="primary">导出报表</Button>-->
                </template>
                <template slot="action" slot-scope="{ row }">
                    <div>
                        <!--
                           处理  isViewBtn ~~审核中 待补件 待补件审核~~
                           编辑 草稿
                           删除 草稿
                       -->
                        <Button :disabled="!row.isViewBtn" type="primary" size="small"
                                @click="edit('write',row)">处理
                        </Button>

                        <Button :disabled="row.processStatus !== 'draft'" type="primary" size="small"
                                @click="edit('write',row)">编辑
                        </Button>

                        <Button :disabled="row.processStatus !== 'draft'" type="error" size="small"
                                @click="remove(row)">
                            删除
                        </Button>
                        <Button v-if="row.processStatus === 'approved' && row.type !== 'insuranceRenew'" type="primary"
                                size="small" @click="PrintTemplate('yes',row)">打印
                        </Button>
                    </div>
                </template>
                <template slot="contractNo" slot-scope="{ row }">
                    <a @click="edit('read',row)">{{row.contractNo}}</a>
                </template>
                <template slot="isSend" slot-scope="{ row }">
                    <Icon v-if="row.isSend" type="md-checkmark" size="16"/>
                </template>
            </rui-table>
            <ActivatedCase
                :isSP="isSP"
                :contractBusiness="contractBusiness"
                :handleBusiness="'insurance'"
                :dataDict="dataDict"
                :modal="modal"
                :modalTitle="modalTitle"
                @showContractDetial="showContractDetial"
                @closeModal="closeModal"
            />
        </rui-page>
    </div>
</template>
<script>
    import {channelType} from "_p/basic/api/contract/channel-type/channel-type";
    import insuranceManageDefine from "./define/insurance-manage-define";
    import {
        delById,
        getInsuranceInfoList,
        PrintHaveTemplate,
        PrintTemplate
    } from "@/projects/basic/api/contract/insurance/insuranceManage";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import ActivatedCase from "_p/basic/pages/contract/common/activated-case/index";
    import {mapGetters} from "vuex";

    export default {
        components: {
            ActivatedCase,
        },
        data() {
            return {
                params: '',
                reparationTypeDict: [], // 赔偿款类型
                modalTitle: "",
                modal: false,
                contractBusiness: ["insurance"],
                businessType: '',
                dicttArr: ["PersonRoleType", "contractStatus", "channelBelong"],
                // selct 数据字典
                dataDict: {},
                searchFormOptions: [
                    {
                        defineId: "insuranceManageDefine",
                        fields: ["applyNo", "type", "custName"]
                    }
                ],
                searchFormOptionsRows: [
                    {
                        defineId: "insuranceManageDefine",
                        fields: ["createTimes", "endDates", "processStatus", "contractNo", "channelFullName",
                            "businessType", "isSend", "businessSource", "dispatchTimes", "channelBelong", "caseAll"],
                        slots: [{key: 'createTimes', slotName: 'createTimes'},
                            {key: 'caseAll', slotName: 'caseAll'},
                            {key: 'endDates', slotName: 'endDates'},
                            {key: 'dispatchTimes', slotName: 'dispatchTimes'}
                        ]
                    }
                ],
                insuranceManageColumns: [
                    'action', 'applyNo', 'processStatus', 'custName', 'type', 'channelFullName', 'contractNo', 'currentAbsoluteOverdue', 'approvalOpinion',
                    'applyDate', 'dispatchTime', 'endDate', 'contractStatus', 'isSend', 'businessSource', "channelBelong", "carLossState", "reparationType", 'processPerson'],
                insuranceManageColumn: ['action', 'applyNo', 'contractNo', 'processStatus', 'custName', 'type', 'approvalOpinion', 'applyDate',
                    'isSend', 'trackingCompany', 'trackingNo', 'reportNum'],
                selectCount: 0,
            };
        },
        computed: {
            ...mapGetters({
                userInfo: "userInfo",
                roleType: "roles",
            }),
            defines() {
                return [
                    {
                        id: "insuranceManageDefine",
                        fields: insuranceManageDefine
                    }
                ];
            },
            // 是否是合作商
            isSP: function () {
                return channelType(this.$route.meta.params);
            }
        },
        mounted() {
            this.init();
        },
        activated() {
            this.refresh();
        },
        methods: {
            init() {
                if (this.isSP) {
                    this.$refs.searchForm.setFiledHide("contractNo", true);
                    this.$refs.searchForm.setFiledHide("isSend", true);
                    this.$refs.searchForm.setFiledHide("endDates", true);
                    this.$refs.searchForm.setFiledHide("businessSource", true);
                    this.$refs.searchForm.setFiledHide("channelFullName", true);
                    this.$refs.searchForm.setFiledHide("dispatchTimes", true);
                    this.$refs.searchForm.setFiledHide("businessType", true);
                }
                // 数据字典
                this.getDictDataList();
            },
            rowClassName(row, index) {
                if (row.currentAbsoluteOverdue > 0) {
                    this.$refs.insuranceTable.getTable().stripe = false;
                    return 'demo-table-error-row';
                }
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    this.dataDict = res;
                });
            },
            selectCreateDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startCreateDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endCreateDate", v[1]);
                }
            },
            selectEndDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startFinishDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endFinishDate", v[1]);
                }
            },
            selectUpdateDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startDispatchDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endDispatchDate", v[1]);
                }
            },
            queryData(queryData) {
                if (queryData.condition.processStatus === '') {
                    queryData.condition.processStatus = null;
                }
                if (queryData.condition.type === '') {
                    queryData.condition.type = null;
                }
                if (queryData.condition.businessType === '') {
                    queryData.condition.businessType = null;
                }
                if (queryData.condition.businessSource === '') {
                    queryData.condition.businessSource = null;
                }
                if (queryData.condition.channelBelong === '') {
                    queryData.condition.channelBelong = null;
                }
                if (queryData.condition.caseAll === null || queryData.condition.caseAll === undefined || queryData.condition.caseAll === "") {
                    queryData.condition.caseAll = false;
                }
                getInsuranceInfoList(queryData, this.params.request).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.insuranceTable.updateTableData(records, total);
                    }
                });
            },
            changeSwitch() {
                this.queryData(this.$refs.searchForm.getFormData())
            },
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            refresh() {
                this.$refs['insuranceTable'].reloadData();
                this.clearSelectAll();
            },
            clearSelectAll() {
                this.$refs.insuranceTable.getTable().selectAll(false);
            },
            showModal() {
                this.modalTitle = "已激活案件列表";
                this.modal = true;
            },
            closeModal() {
                this.modal = false;
            },
            PrintTemplate(type, row) {
                let self = this;
                if (type === 'yes') {
                    PrintHaveTemplate(row, this.params.request).then(res => {
                        this.$Message.success("打印成功！");
                        self.afs.downloadFile(res);
                    });
                } else if (type === 'no') {
                    PrintTemplate(row, this.params.request).then(res => {
                        this.$Message.success("打印成功！");
                        self.afs.downloadFile(res);
                    });
                }
            },
            showContractDetial(params) {
                params.editFlag = false;
                params.isSP = this.isSP;
                params.request = this.params.request;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/insurance/insuranceInfoView",
                    "保险业务详情",
                    "ios-add",
                    params,
                    "contract-" + params.id,
                    [],
                    true
                );
            },
            edit(type, params) {
                params.readOnly = type;
                params.editFlag = true;
                params.isSP = this.isSP;
                params.request = this.params.request;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/insurance/insuranceInfoView",
                    "保险业务详情",
                    "ios-add",
                    params,
                    "contract-" + params.id,
                    [],
                    true
                );
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除单号 " + v.applyNo + " ?",
                    onOk: () => {
                        delById(v.id, this.params.request).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.refresh();
                            }
                        });
                    }
                });
            },
        }
    };
</script>
<style>
    .ivu-table .demo-table-error-row td {
        background-color: #ff6600;
        color: #fff;
    }
</style>
